/*
 * WindVane.c
 *
 * Created: 20/02/2012 14:42:22
 *  Author: Dani
 */ 

#include <avr/io.h>

void Vane_Init()
{
	// PA pins are inputs (ADC)
	DDRA &= ~(1);
	// Internal 5 volt reference, ADLAR = 0, Input = ADC0 single ended
	ADMUX = 0b01000000;
	// ADC enable
	// ADC interrupt disabled
	// ADC prescaler = 32 (=> ADC clock = 3,6864 MHz / 32 = 115 kHZ)
	ADCSRA = 0b10000101;
}

uint16_t Vane_Read()
{
	unsigned char i = 0;
	unsigned long WindDirection = 0;
	unsigned int WindReturn = 0;
	
	Vane_Init();
	
	for (i = 0; i < 10; i++)
	{
		// Start new AD conversion
		ADCSRA |= 0b01000000;
		// Wait for AD conversion ended
		while (ADCSRA & 0b01000000)
			{}
		//WindDirection[i] = ADCW;
		WindDirection += ADCW;
	}	
	
	WindReturn = WindDirection*36/1024;
	return WindReturn;
}